package com.example.demo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.mapper.QuestionMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.Question;
import com.example.demo.pojo.User;
import com.example.demo.service.IQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements IQuestionService {

    @Autowired
    QuestionMapper questionMapper;
    @Autowired
    UserMapper userMapper;

    public List<Question> getAllQuestion(){
        return questionMapper.selectList(null);
    }

    public List<Question> getQuestionById(int id){
        QueryWrapper<Question> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        return questionMapper.selectList(queryWrapper);
    }
    public void insertQuestion(Question question,int id){
        Timestamp timestamp=new Timestamp(System.currentTimeMillis());
        question.setQuestion_time(timestamp);
        User user=userMapper.getUserById(id);

        String name=user.getUsername();
        String hp_url=user.getHp_url();
        question.setUsername(name);
        question.setUser_id(id);
        question.setHp_url(hp_url);
        questionMapper.insert(question);
    }


}
